<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>apache配置防盗链 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>apache配置防盗链</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果站点是一个图片站，有很多非常漂亮的美女图片，时间久了会有很多人来网站借图片，有的直接下载走了，还有的直接取走图片的地址，那么他就可以直接把图片地址放到他自己的网站上，他的用户可以直接从他的网站上查看这个图片，而实际上浏览这个图片是从我的网站上访问的。这样，这个图片所产生的宽带开销对我来说没有任何意义，毕竟看图片的人不是我的客户，而是人家的。所以我们要把这些图片限制一下，凡是在第三方站点上，严禁访问我站点的图片。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># vim /usr/local/apache2/conf/extra/httpd-vhosts.conf</span></div><div class="line"></div><div class="line"></div><div class="line">SetEnvIfNoCase Referer <span class="string">"^http://.*\.test\.com"</span> local_ref</div><div class="line">SetEnvIfNoCase Referer <span class="string">".*\.aaa\.com"</span> local_ref</div><div class="line">SetEnvIfNoCase Referer <span class="string">"^$"</span> local_ref</div><div class="line">&lt;filesmatch <span class="string">"\.(txt|doc|mp3|zip|rar|jpg|gif|png|js|css)"</span>&gt;</div><div class="line">  Order Allow,Deny</div><div class="line">  Allow from env=local_ref</div><div class="line">&lt;/filesmatch&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;检测是否有错，并重启apache 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># apachectl -t</span></div><div class="line">Syntax OK</div><div class="line">[root@lamp ~]<span class="comment"># apachectl restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：在这段配置中涉及到一个名词 referer ，它其实就是上次访问的网站连接。我们是根据来源链接做限制的，如果来源链接并不是想要的，就直接拒绝，这就是防盗链的原理。当然不止是图片，mp3，rar，zip等文件同样支持。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Apache/41. apache配置防盗链/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
